package com.cjl.multi_thread.aqs;

import java.util.concurrent.atomic.AtomicStampedReference;

public class UseAtomicStampedRef {
    static AtomicStampedReference<String> asr = new AtomicStampedReference<>("dota",0);

    public static void main(String[] args) throws InterruptedException {
        int stamp = asr.getStamp();
        String reference = asr.getReference();

        System.out.println("UseAtomicStampedRef.main old stamp "+stamp);

        Thread right = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("UseAtomicStampedRef.run "+
                        Thread.currentThread().getName()+" "+
                        reference+"-"+stamp+"      ---> "+
                        asr.compareAndSet(reference,reference+"_no",stamp,stamp+1));
            }
        });

        Thread error = new Thread(new Runnable() {
            @Override
            public void run() {
                String ref = asr.getReference();

                System.out.println("UseAtomicStampedRef.run "+
                        Thread.currentThread().getName()+" "+
                        ref+"-"+asr.getStamp()+"      ---> "+
                        asr.compareAndSet(reference,reference+"_Yes",stamp,stamp+1));
            }
        });

        right.start();
        right.join();

        error.start();;
        error.join();


        System.out.println("UseAtomicStampedRef.main --- "+asr.getReference()+"  "+asr.getStamp());

    }
}
